#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution 
{
public:
    bool isValidBST(TreeNode* root) 
    {
        if(root == nullptr)
        {
            return true;
        }
        stack<pair<TreeNode*, bool>> st;
        st.push(make_pair(root, false));
        int lastValue = 0;
        bool isFirstNode = true;
        while(!st.empty())
        {
            auto node = st.top().first;
            auto visited = st.top().second;
            st.pop();
            if(visited)
            {
                if(isFirstNode || node->val > lastValue)
                {
                    isFirstNode = false;
                    lastValue = node->val;
                    continue;
                }
                else
                {
                    return false;
                }
            }
            if(node->right != nullptr)
            {
                st.push(make_pair(node->right, false));
            }
            st.push(make_pair(node, true));
            if(node->left != nullptr)
            {
                st.push(make_pair(node->left, false));
            }
        }
        return true;
    }
};

// 辅助函数：从数组构建二叉树（层序遍历方式，-1表示null）
TreeNode* buildTreeFromArray(vector<int>& arr) {
    if (arr.empty() || arr[0] == -1) return nullptr;
    
    TreeNode* root = new TreeNode(arr[0]);
    queue<TreeNode*> q;
    q.push(root);
    
    int i = 1;
    while (!q.empty() && i < arr.size()) {
        TreeNode* node = q.front();
        q.pop();
        
        // 左子节点
        if (i < arr.size() && arr[i] != -1) {
            node->left = new TreeNode(arr[i]);
            q.push(node->left);
        }
        i++;
        
        // 右子节点
        if (i < arr.size() && arr[i] != -1) {
            node->right = new TreeNode(arr[i]);
            q.push(node->right);
        }
        i++;
    }
    
    return root;
}

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode* root) {
    if (!root) {
        cout << "[]" << endl;
        return;
    }
    
    queue<TreeNode*> q;
    q.push(root);
    vector<string> result;
    
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        
        if (node) {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else {
            result.push_back("null");
        }
    }
    
    // 移除末尾的null
    while (!result.empty() && result.back() == "null") {
        result.pop_back();
    }
    
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    cout << "=== LeetCode 98. 验证二叉搜索树 测试案例 ===" << endl << endl;
    
    // 测试案例1：题目示例1 - 有效BST
    cout << "测试案例1（有效BST）：" << endl;
    vector<int> tree1 = {2, 1, 3};
    TreeNode* root1 = buildTreeFromArray(tree1);
    
    cout << "输入: root = ";
    printTree(root1);
    
    bool result1 = solution.isValidBST(root1);
    cout << "输出: " << (result1 ? "true" : "false") << endl;
    cout << "期望: true" << endl << endl;
    
    // 测试案例2：题目示例2 - 无效BST
    cout << "测试案例2（无效BST）：" << endl;
    vector<int> tree2 = {5, 1, 4, -1, -1, 3, 6};
    TreeNode* root2 = buildTreeFromArray(tree2);
    
    cout << "输入: root = ";
    printTree(root2);
    
    bool result2 = solution.isValidBST(root2);
    cout << "输出: " << (result2 ? "true" : "false") << endl;
    cout << "期望: false" << endl << endl;
    
    // 测试案例3：单节点树
    cout << "测试案例3（单节点树）：" << endl;
    vector<int> tree3 = {1};
    TreeNode* root3 = buildTreeFromArray(tree3);
    
    cout << "输入: root = ";
    printTree(root3);
    
    bool result3 = solution.isValidBST(root3);
    cout << "输出: " << (result3 ? "true" : "false") << endl;
    cout << "期望: true" << endl << endl;
    
    // 测试案例4：左子树违反BST性质
    cout << "测试案例4（左子树违反BST性质）：" << endl;
    vector<int> tree4 = {5, 4, 6, -1, -1, 3, 7};
    TreeNode* root4 = buildTreeFromArray(tree4);
    
    cout << "输入: root = ";
    printTree(root4);
    
    bool result4 = solution.isValidBST(root4);
    cout << "输出: " << (result4 ? "true" : "false") << endl;
    cout << "期望: false" << endl << endl;
    
    // 测试案例5：包含重复值的树
    cout << "测试案例5（包含重复值）：" << endl;
    vector<int> tree5 = {1, 1};
    TreeNode* root5 = buildTreeFromArray(tree5);
    
    cout << "输入: root = ";
    printTree(root5);
    
    bool result5 = solution.isValidBST(root5);
    cout << "输出: " << (result5 ? "true" : "false") << endl;
    cout << "期望: false" << endl << endl;
    
    // 测试案例6：较大的有效BST
    cout << "测试案例6（较大的有效BST）：" << endl;
    vector<int> tree6 = {10, 5, 15, 3, 7, 12, 20};
    TreeNode* root6 = buildTreeFromArray(tree6);
    
    cout << "输入: root = ";
    printTree(root6);
    
    bool result6 = solution.isValidBST(root6);
    cout << "输出: " << (result6 ? "true" : "false") << endl;
    cout << "期望: true" << endl << endl;
    
    // 测试案例7：边界值测试
    cout << "测试案例7（边界值测试）：" << endl;
    vector<int> tree7 = {2147483647};
    TreeNode* root7 = buildTreeFromArray(tree7);
    
    cout << "输入: root = ";
    printTree(root7);
    
    bool result7 = solution.isValidBST(root7);
    cout << "输出: " << (result7 ? "true" : "false") << endl;
    cout << "期望: true" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root3);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);
    
    cout << "所有测试案例执行完毕！" << endl;
    
    return 0;
}